package com.hui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hui.entity.dao.StoreProductAttr;
import com.hui.entity.dao.StoreProductAttrValue;
import com.hui.entity.dto.AttrValueDto;
import com.hui.entity.enums.ProductTypeEnum;
import com.hui.entity.vo.StoreProductAttrQueryVo;
import com.hui.mapper.StoreProductAttrMapper;
import com.hui.mapper.StoreProductAttrValueMapper;
import com.hui.service.StoreProductAttrService;
import com.hui.service.StoreProductAttrValueService;
import com.hui.util.BeanCopyUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品属性表(StoreProductAttr)表服务实现类
 *
 * @author hui
 * @since 2022-07-17 11:21:04
 */
@RequiredArgsConstructor
@Service("storeProductAttrService")
public class StoreProductAttrServiceImpl extends ServiceImpl<StoreProductAttrMapper, StoreProductAttr> implements StoreProductAttrService {

    //    private  StoreProductAttrValueMapper storeProductAttrValueMapper;
    private final StoreProductAttrValueService storeProductAttrValueService;
    private final StoreProductAttrValueMapper yxStoreProductAttrValueMapper;

    /***
     * 获取商品的规格信息
     * @param productId
     * @return
     */
    @Override
    public Map<String, Object> getProductAttrDetail(long productId) {
        /**
         * 获取商品的属性
         */
        LambdaQueryWrapper<StoreProductAttr> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StoreProductAttr::getProductId, productId)
                .orderByAsc(StoreProductAttr::getAttrValues);
        List<StoreProductAttr> storeProductAttrs = list(wrapper);
        /**
         * 获取商品的规格信息
         */
        LambdaQueryWrapper<StoreProductAttrValue> wrapper2 = new LambdaQueryWrapper();
        wrapper2.eq(StoreProductAttrValue::getProductId, productId);
        List<StoreProductAttrValue> storeProductAttrValues = storeProductAttrValueService.list(wrapper2);//找到商品所有的规格信息

        //将商品规格的名字作为map的key进行封装
        Map<String, StoreProductAttrValue> AttrValuesMap = (Map) storeProductAttrValues.stream().collect(Collectors.toMap(StoreProductAttrValue::getSku, (p) -> {
            return p;
        }));


        /**
         *  封装商品规格
         */
        //总列表
        List<StoreProductAttrQueryVo> storeProductAttrQueryVoList = new ArrayList();

        for (StoreProductAttr attr : storeProductAttrs) {
            List<String> stringList = Arrays.asList(attr.getAttrValues().split(","));//将单个对象的属性值，进行分割
            //将单个属性值封装成list
            List<AttrValueDto> attrValueDTOS = new ArrayList();
            for (String str : stringList) {
                AttrValueDto attrValueDTO = new AttrValueDto();
                attrValueDTO.setAttr(str);
                attrValueDTOS.add(attrValueDTO);
            }

            StoreProductAttrQueryVo attrQueryVo = BeanCopyUtils.copyBean(attr, StoreProductAttrQueryVo.class);
            attrQueryVo.setAttrValue(attrValueDTOS);
            attrQueryVo.setAttrValueArr(stringList);
            storeProductAttrQueryVoList.add(attrQueryVo);
        }

        Map<String, Object> returnMap = new LinkedHashMap(2);

        returnMap.put("productAttr", storeProductAttrQueryVoList);
        returnMap.put("productValue", AttrValuesMap);

        return returnMap;
    }

    @Override
    public void incProductAttrStock(Integer num, Long productId, String unique, String type) {
        if (ProductTypeEnum.COMBINATION.getValue().equals(type)) {
            this.yxStoreProductAttrValueMapper.incCombinationStockDecSales(num, productId, unique);
        } else if (ProductTypeEnum.SECKILL.getValue().equals(type)) {
            this.yxStoreProductAttrValueMapper.incSeckillStockDecSales(num, productId, unique);
        } else {
            this.yxStoreProductAttrValueMapper.incStockDecSales(num, productId, unique);
        }

    }
}

